package com.briup.day41;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

// 聊天室客户端 - 版本1
public class ChatClient1 {
	
	public static void main(String[] args) {
		
		String ip = "localhost";
		int port = 8888;
		try {
			// 创建客户端套接字对象
			// 注意：这里的ip实际上定义的是服务器的IP地址
			Socket socket = new Socket(ip, port);
			System.out.println("连接服务器成功！");
			
			// 向服务器发送一条信息
			OutputStream os = socket.getOutputStream();
			// String msg = "Nice to meet you, Server!";
			String msg = "你好啊！";
			os.write(MessageUtil.toMessage(msg).getBytes());
			os.flush();
			socket.shutdownOutput(); // 为了让对方能够接收到-1
			
			// 接收服务器回复的信息
			InputStream is = socket.getInputStream();
			System.out.println("【服务器说】" + MessageUtil.parseMessage(is));
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
}
